//
//  AddressManager.m
//  Dresshere
//
//  Created by braspring on 2017/12/5.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "AddressManager.h"
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>

@interface AddressManager ()<AMapLocationManagerDelegate>

@property (strong, nonatomic) AMapLocationManager *locationManager;

@end

@implementation AddressManager

static AddressManager *_shareClient = nil;
static dispatch_once_t onceToken;

+ (AddressManager *)shareAddressManager{
    dispatch_once(&onceToken, ^{
        _shareClient = [[AddressManager alloc] init];
        [AMapServices sharedServices].apiKey = AMAP_KEY;
    });
    return _shareClient;
}

- (void)updateCurrentLocationAndBlock:(void (^)(id, NSError *))block{
    if (self.locationManager == nil){
        self.locationManager = [[AMapLocationManager alloc] init];
        [self.locationManager setDelegate:self];
        // 带逆地理信息的一次定位（返回坐标和地址信息）
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
        //   定位超时时间，最低2s，此处设置为2s
        self.locationManager.locationTimeout =2;
        //   逆地理请求超时时间，最低2s，此处设置为2s
        self.locationManager.reGeocodeTimeout = 2;
    }
    
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (error){
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            if (error.code == AMapLocationErrorLocateFailed){
                block(nil, error);
            }
        }
        if (regeocode){
            NSLog(@"reGeocode:%@", regeocode);
//            if (regeocode.city) {
//                NSMutableString *temp = regeocode.city.mutableCopy;
//                self.city = [temp stringByReplacingOccurrencesOfString:@"市" withString:@""];
//            }else{
//                self.city = @"北京";
//            }
            self.city = regeocode.city? : @"北京市";
            self.district = regeocode.district? : @"丰台区";
            self.province = regeocode.province? : @"北京市";
            self.address = regeocode.street? : @"北京西站";
            self.formattedAddress = regeocode.formattedAddress? : @"北京市丰台区北京西站";
            
            block(@"", error);
//            if (regeocode.province) {
//                GeoCode *geoCode = [[GeoCode alloc] init];
//                geoCode.province = regeocode.province? : @"";
//                geoCode.city = regeocode.city? : @"";
//                geoCode.district = regeocode.district? : @"";
//                Location *currentLocation = [[Location alloc] init];
//                [currentLocation.coordinates addObject:@(location.coordinate.longitude)];
//                [currentLocation.coordinates addObject:@(location.coordinate.latitude)];
//                NSDictionary *dic = @{@"address":[NSString stringWithFormat:@"%@%@%@",regeocode.province,regeocode.city,regeocode.district],
//                                      @"detail_address":[NSString stringWithFormat:@"%@%@",regeocode.street,regeocode.number],
//                                      @"geocode":[geoCode JSONString],
//                                      @"location":[currentLocation JSONString]
//                                      };
//            }else{
//
//            }
            
        }
    }];
}

@end
